package com.sap.mobile.maf.samples.android.mafprovisioningsample;

import java.lang.reflect.Array;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.sap.maf.uicontrols.view.MAFActionBarItem;
import com.sap.maf.uicontrols.view.MAFSearchBox;
import com.sap.maf.uicontrols.view.MAFSectionHeader;
import com.sap.maf.uicontrols.view.MAFTextView;

public class SampleTabContent extends Activity {

	private MAFTextView tv;
	private MAFSectionHeader header;
	private MAFSearchBox sBox;
	private SearchActionBarItem sABI;
	private DoneActionBarItem dABI;
	private static String nameStr="";
	private static String addressStr="";
	private static String phoneStr="";
	private static boolean dataReceived=false;
	private static DataListener dlr=null;
	
	public static boolean isDataReceived() {
		return dataReceived;
	}

	public static void setDataReceived(boolean dataReceived) {
		SampleTabContent.dataReceived = dataReceived;
	}

	public static String getNameStr() {
		return nameStr;
	}

	public static void setNameStr(String nameStr) {
		SampleTabContent.nameStr = nameStr;
	}

	public static String getAddressStr() {
		return addressStr;
	}

	public static void setAddressStr(String addressStr) {
		SampleTabContent.addressStr = addressStr;
	}

	public static String getPhoneStr() {
		return phoneStr;
	}

	public static void setPhoneStr(String phoneStr) {
		SampleTabContent.phoneStr = phoneStr;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LinearLayout ll = new LinearLayout(this);
		LinearLayout row0 = new LinearLayout(this);
		row0.setOrientation(LinearLayout.HORIZONTAL);
		row0.setGravity(Gravity.CENTER_HORIZONTAL);
		
		ll.setOrientation(LinearLayout.VERTICAL);
		ll.setGravity(Gravity.CENTER_HORIZONTAL);
		
		Button btn = new Button(this);
		btn.setId("get".hashCode());
		btn.setText("Get Data");
		btn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if(getData()){
					dataReceived=true;
				};
			}
		});
		
		Button btn2 = new Button(this);
		btn2.setId("clear".hashCode());
		btn2.setText("Clear Data");
		btn2.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if(dataReceived){
					dataReceived=false;
					findViewById("get".hashCode()).setEnabled(true);
					findViewById("clear".hashCode()).setEnabled(false);
					if(dlr!=null){
						dlr.onDataReceived(isDataReceived());
					}
					showLayout(false);
				}
			}
		});
		btn2.setEnabled(false);
		btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,0.5f));
		btn2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,0.5f));
		btn.setPadding(0, 0, 15, 0);
		btn2.setPadding(15, 0, 0, 0);
		row0.setPadding(30, 30, 30, 30);
		row0.addView(btn);
		row0.addView(btn2);
		ll.addView(row0);
		
		
		LinearLayout row1 = new LinearLayout(this);
		row1.setId(1);
		row1.setOrientation(LinearLayout.HORIZONTAL);
		row1.setGravity(Gravity.LEFT);
		
	
		ImageView iv = new ImageView(this);
		iv.setImageResource(R.drawable.portrait);
		LinearLayout r1c1 = new LinearLayout(this);
		r1c1.setLayoutParams(new LayoutParams(150,150));
		//r1c1.setPadding(30, 30, 30, 30);
		r1c1.addView(iv);
		
		row1.addView(r1c1);
		
		LinearLayout r1c2 = new LinearLayout(this);
		r1c2.setOrientation(LinearLayout.VERTICAL);
		r1c2.setGravity(Gravity.LEFT);
		TextView nameTitle = new TextView(this);
		TextView addressTitle = new TextView(this);
		TextView phoneTitle = new TextView(this);
		
		
		nameTitle.setText("Name: ");
		addressTitle.setText("Address: ");
		phoneTitle.setText("Phone: ");
//		r1c2.addView(nameTitle);
//		r1c2.addView(addressTitle);
//		r1c2.addView(phoneTitle);
		
		LinearLayout r1c3 = new LinearLayout(this);
		r1c3.setOrientation(LinearLayout.VERTICAL);
		r1c3.setGravity(Gravity.LEFT);
		r1c3.setPadding(30, 0, 30, 0);
		TextView name = new TextView(this);
		name.setId("name".hashCode());
		TextView address = new TextView(this);
		address.setId("adr".hashCode());
		TextView phone = new TextView(this);
		phone.setId("phone".hashCode());
		
		
		name.setText(nameStr);
		address.setText(addressStr);
		phone.setText(phoneStr);

		TableLayout tl = new TableLayout(this);
		tl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT));
		TableRow tv1 = new TableRow(this);
		TableRow tv2 = new TableRow(this);
		TableRow tv3 = new TableRow(this);
		tv1.addView(nameTitle);
		tv1.addView(name);
		tv2.addView(addressTitle);
		tv2.addView(address);
		tv3.addView(phoneTitle);
		tv3.addView(phone);
		tl.addView(tv1);
		tl.addView(tv2);
		tl.addView(tv3);
		row1.addView(tl);
		
		
		
		
		ll.addView(row1);
		TextView matReq = new TextView(this);
		matReq.setId(3);
		matReq.setText("MATERIAL REQUIREMENTS");
		matReq.setTypeface(null, Typeface.BOLD);
		matReq.setTextSize(20);
		matReq.setPadding(0, 50, 0, 30);
		matReq.setGravity(Gravity.CENTER_HORIZONTAL);
		ll.addView(matReq);
		TableLayout row2 = new TableLayout(this);
		row2.setGravity(Gravity.CENTER);
		row2.setPadding(30, 0, 30, 0);
		row2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		row2.setId(2);
		row2.setStretchAllColumns(true);
		TableRow r2r1 = new TableRow(this);
		TextView partIdTitle = new TextView(this);
		partIdTitle.setText("ID");
		partIdTitle.setTypeface(null, Typeface.BOLD);
		partIdTitle.setGravity(Gravity.CENTER_HORIZONTAL);
		TextView partTitle = new TextView(this);
		partTitle.setText("Part Name");
		partTitle.setTypeface(null, Typeface.BOLD);
		partTitle.setGravity(Gravity.CENTER_HORIZONTAL);
		TextView partQtTitle = new TextView(this);
		partQtTitle.setText("Quantity");
		partQtTitle.setTypeface(null, Typeface.BOLD);
		partQtTitle.setGravity(Gravity.CENTER_HORIZONTAL);
		r2r1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		r2r1.setGravity(Gravity.CENTER_HORIZONTAL);
		r2r1.addView(partIdTitle);
		r2r1.addView(partTitle);
		r2r1.addView(partQtTitle);
		row2.addView(r2r1);
		
		
		ll.addView(row2);
		setContentView(ll);
		showLayout(false);
	}

	private void showMaterialData(ArrayList<Material> mats){
		TableLayout row2 = (TableLayout)findViewById(2);
		row2.removeAllViews();
		TableRow r2r1 = new TableRow(this);
		TextView partIdTitle = new TextView(this);
		partIdTitle.setText("ID");
		partIdTitle.setTypeface(null, Typeface.BOLD);
		partIdTitle.setGravity(Gravity.CENTER_HORIZONTAL);
		TextView partTitle = new TextView(this);
		partTitle.setText("Part Name");
		partTitle.setTypeface(null, Typeface.BOLD);
		partTitle.setGravity(Gravity.CENTER_HORIZONTAL);
		TextView partQtTitle = new TextView(this);
		partQtTitle.setText("Quantity");
		partQtTitle.setTypeface(null, Typeface.BOLD);
		partQtTitle.setGravity(Gravity.CENTER_HORIZONTAL);
		r2r1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		r2r1.setGravity(Gravity.CENTER_HORIZONTAL);
		r2r1.addView(partIdTitle);
		r2r1.addView(partTitle);
		r2r1.addView(partQtTitle);
		row2.addView(r2r1);
		TableRow tempRow;
		TextView tempText;
		for(Material k : mats){
			tempRow = new TableRow(this);
			tempRow.setGravity(Gravity.CENTER_HORIZONTAL);
			tempText = new TextView(this);
			tempText.setText(Integer.toString(k.getId()));
			tempRow.addView(tempText);
			tempText = new TextView(this);
			tempText.setText(k.getName());
			tempRow.addView(tempText);
			tempText = new TextView(this);
			tempText.setText(Integer.toString(k.getQuantity()));
			tempRow.addView(tempText);
			row2.addView(tempRow);
		}
	}
	
	private class Material{
		int id;
		String name;
		int quantity;
		public Material(int _id,String _mat,int qt){
			id=_id;
			name=_mat;
			quantity=qt;
		}
		
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getQuantity() {
			return quantity;
		}
		public void setQuantity(int quantity) {
			this.quantity = quantity;
		}
		
	}
		
	
	private ArrayList<Material> getMatData(){
		ArrayList<Material> retList = new ArrayList<Material>();
		retList.add(new Material(42365,"Coax cable - meter",2));
		retList.add(new Material(32961,"Set Top Box - pcs",1));
		retList.add(new Material(74683,"Contract no.CUST94 - pcs",1));
		return retList;
	}
	
	public boolean getData(){
		nameStr = "Kovács Kornél";
		addressStr = "Budapest VI. Szinyei Merse utca 29.";
		phoneStr = "+36-70-337-0284";
		TextView tv = (TextView)findViewById("name".hashCode());
		tv.setText(nameStr);
		TextView tv2 = (TextView)findViewById("adr".hashCode());
		tv2.setText(addressStr);
		TextView tv3 = (TextView)findViewById("phone".hashCode());
		tv3.setText(phoneStr);
		showLayout(true);
		findViewById("get".hashCode()).setEnabled(false);
		findViewById("clear".hashCode()).setEnabled(true);
		dataReceived = true;
		if(dlr!=null){
			dlr.onDataReceived(true);
		}
		showMaterialData(getMatData());
		return true;
	}
	
	public void showLayout(boolean show){
		if(show){
			findViewById(1).setVisibility(LinearLayout.VISIBLE);
			findViewById(2).setVisibility(LinearLayout.VISIBLE);
			findViewById(3).setVisibility(LinearLayout.VISIBLE);
		}else{
			findViewById(1).setVisibility(LinearLayout.GONE);
			findViewById(2).setVisibility(LinearLayout.GONE);
			findViewById(3).setVisibility(LinearLayout.GONE);
		}
	}
	public static String getAddressFormed(){
		if(SampleTabContent.isDataReceived()){
			return SampleTabContent.getAddressStr().replace(" ", "+");
		}else{
			return "";
		}
		
	}
	private class SearchActionBarItem extends MAFActionBarItem {

		public SearchActionBarItem(int id, int iconId, int textId) {
			super(id, iconId, textId);
		}

		@Override
		public void performAction() {
			sBox.setVisibility(View.VISIBLE);
			header.addAction(dABI);
		}

	}

	public static void registerForData(DataListener dl){
		dlr = dl;
	}
	
	public static interface DataListener{
		
		public void onDataReceived(boolean dataReceived);
		
	};
	
	private class DoneActionBarItem extends MAFActionBarItem {

		public DoneActionBarItem(int id, int iconId, int textId) {
			super(id, iconId, textId);
		}

		@Override
		public void performAction() {
			sBox.setVisibility(View.GONE);
			header.addAction(sABI);
		}
	}
}
